
//
//  MyShopServiceTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/12.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyShopServiceTabVC.h"
#import "MyShopServiceCell.h"
#import "ServiceModel.h"
#import "WriteRequirementTitleCell.h"
@interface MyShopServiceTabVC ()<MyShopServiceCellDelegate>
@property(nonatomic,strong) NSDictionary  *dict;
@property(nonatomic,strong) NSMutableArray *onlineServiceASrray;
@property(nonatomic,strong) NSMutableArray *offlineServiceASrray;
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,assign) NSInteger row;
@end

static NSString *identifierTitle = @"titleCell";
static NSString *identifierService = @"MyShopServiceCell";
@implementation MyShopServiceTabVC
-(NSMutableArray *)onlineServiceASrray
{
    if (!_onlineServiceASrray) {
        _onlineServiceASrray = [NSMutableArray array];
    }
    return _onlineServiceASrray;
}
-(NSMutableArray *)offlineServiceASrray
{
    if (!_offlineServiceASrray) {
        _offlineServiceASrray = [NSMutableArray array];
    }
    return _offlineServiceASrray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(associationshop:) name:@"associationshop" object:nil];
    [self.tableView registerNib:[UINib nibWithNibName:@"WriteRequirementTitleCell" bundle:nil] forCellReuseIdentifier:identifierTitle];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyShopServiceCell" bundle:nil] forCellReuseIdentifier:identifierService];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(associationdelete:) name:@"associationdelete" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(associationoff:) name:@"associationoff" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(associationup:) name:@"associationup" object:nil];
    self.page = 1;
    self.row = 10;
    [self reloadInternetData];
}
-(void)reloadInternetData
{
    self.onlineServiceASrray = nil;
    self.offlineServiceASrray = nil;
    NSString *pages = [NSString stringWithFormat:@"%ld",self.page];
    NSString *rows = [NSString stringWithFormat:@"%ld",self.row];
    /**
     WSProgressHUDIndicatorCustom,
     WSProgressHUDIndicatorMMSpinner,
     WSProgressHUDIndicatorSmallLight,
     WSProgressHUDIndicatorGray,
     WSProgressHUDIndicatorBigGray,
     */
//    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
//    [WSProgressHUD show];
    self.dict = @{
                  @"uid":[UserDefaultsHelper readUid],
                  @"page":pages,
                  @"rows":rows,
                  @"token":[UserDefaultsHelper readToken]
                  };
    XLog(@"self.dict:%@",self.dict);
    [CustomeManager POSTWithURLIndex:51 parameters:self.dict success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}
-(void)associationshop:(NSNotification *)info
{
    
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSDictionary *date = [dict objectForKey:@"date"];
    NSArray *isShelf = [date objectForKey:@"isShelf"];
    NSArray *isNotShelf = [date objectForKey:@"isNotShelf"];
    if (isShelf.count) {
        for (NSDictionary *dict in isShelf) {
            ServiceModel *model = [ServiceModel mj_objectWithKeyValues:dict];
            [self.onlineServiceASrray addObject:model];
        }
    }
    if (isNotShelf.count) {
        for (NSDictionary *dict in isNotShelf) {
            ServiceModel *model = [ServiceModel mj_objectWithKeyValues:dict];
            [self.offlineServiceASrray addObject:model];
        }
    }
    [self.tableView reloadData];
    [WSProgressHUD dismiss];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.onlineServiceASrray.count?self.onlineServiceASrray.count+1:0;
    }else {
        return self.offlineServiceASrray.count?self.offlineServiceASrray.count+1:0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    if (indexPath.row == 0) {
        WriteRequirementTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierTitle forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        switch (indexPath.section) {
            case 0:
                cell.titleL.text = @"已上架服务";
                break;
                case 1:
                cell.titleL.text = @"未上架服务";
                break;
            default:
                break;
                
        }
        return cell;
    }else {
        MyShopServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierService forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.indexPath = indexPath;
        cell.delegate = self;
        switch (indexPath.section) {
            case 0:
            {
                [cell.onOffLineBtn setTitle:@"下架" forState:UIControlStateNormal];
                [cell.onOffLineBtn setTitleColor:kLabel102Color forState:UIControlStateNormal];
                ServiceModel *model = self.onlineServiceASrray[indexPath.row -1];
                cell.serviceModel = model;
            }
                break;
            case 1:
            {
                [cell.onOffLineBtn setTitle:@"上架" forState:UIControlStateNormal];
                [cell.onOffLineBtn setTitleColor:[UIColor colorWithRed:255/255.0 green:152/255.0 blue:0 alpha:1] forState:UIControlStateNormal];
                ServiceModel *model = self.offlineServiceASrray[indexPath.row -1];
                cell.serviceModel = model;
                
            }
                break;
            default:
                break;
        }

        return cell;
    }
        
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        if (indexPath.row != 0) {
            ServiceModel *serviceModel = self.onlineServiceASrray[indexPath.row-1];
            if (self.returnValueBlock) {
                self.returnValueBlock(serviceModel);
            };
        }
    }else {
        if (indexPath.row != 0) {
            ServiceModel *serviceModel = self.offlineServiceASrray[indexPath.row-1];
            if (self.returnValueBlock) {
                self.returnValueBlock(serviceModel);
            };            
        }
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 60;
    }else {
        return 130;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 1)];
    return headerView;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0,1)];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
#pragma mark-代理:删除,下架,修改,操作
- (void)myShopServiceCell:(MyShopServiceCell *)myShopServiceCell withBtn:(UIButton *)btn withIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ServiceModel *model = self.onlineServiceASrray[indexPath.row -1];
        NSString *ids = [NSString stringWithFormat:@"%ld",model.id];
        self.dict = @{
                      @"uid":[UserDefaultsHelper readUid],
                      @"token":[UserDefaultsHelper readToken],
                      @"aid":ids,
                      };
        if (btn.tag == 0) {
        
            [CustomeManager POSTWithURLIndex:57 parameters:self.dict success:^(NSDictionary *dict) {
                
            } failure:^(NSError *error) {
                
            }];
    
        }else if (btn.tag == 1) {
            [CustomeManager POSTWithURLIndex:58 parameters:self.dict success:^(NSDictionary *dict) {
                
            } failure:^(NSError *error) {
                
            }];
        }
        
    }else {
        ServiceModel *model = self.offlineServiceASrray[indexPath.row -1];
        NSString *ids = [NSString stringWithFormat:@"%ld",model.id];
        self.dict = @{
                      @"uid":[UserDefaultsHelper readUid],
                      @"token":[UserDefaultsHelper readToken],
                      @"aid":ids,
                      };
        if (btn.tag == 0) {
            [CustomeManager POSTWithURLIndex:57 parameters:self.dict success:^(NSDictionary *dict) {
                
            } failure:^(NSError *error) {
                
            }];
        }else if (btn.tag == 1) {
            [CustomeManager POSTWithURLIndex:59 parameters:self.dict success:^(NSDictionary *dict) {
                
            } failure:^(NSError *error) {
                
            }];
        }
    }
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
#pragma mark-代理:删除
-(void)associationdelete:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [WSProgressHUD dismiss];
        [self reloadInternetData];
    });
}
-(void)associationoff:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self reloadInternetData];
    });
}
-(void)associationup:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self reloadInternetData];
    });
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    XLog(@"scrollView");
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}
@end
